package com.hg.library.utils;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;

import java.io.File;

/**
 * Created by A on 2016/3/31.
 */
public class StorageSizeUtil {
    private static StorageSizeUtil util = new StorageSizeUtil();

    private static File sdPath = Environment.getExternalStorageDirectory();

    private static long sdBlockSize;
    private static long sdBlockCount;
    private static long sdAvailableBlocks;


    private static long exBlockSize;
    private static long exBlockCount;
    private static long exAvailableBlocks;

    public static StorageSizeUtil getInstance() {
        StatFs sdSf = new StatFs(sdPath.getPath());
        sdBlockSize = sdSf.getBlockSizeLong();
        sdBlockCount = sdSf.getBlockCountLong();
        sdAvailableBlocks = sdSf.getAvailableBlocksLong();
        return util;
    }

    public static StorageSizeUtil getInstance(String path) {
        StatFs exSF = new StatFs(path);
        exBlockSize = exSF.getBlockSizeLong();
        exBlockCount = exSF.getBlockCountLong();
        exAvailableBlocks = exSF.getAvailableBlocksLong();
        return util;
    }

    public String getSDUsedSize(Context context) {
        return formatSize(context, sdBlockSize * (sdBlockCount - sdAvailableBlocks));
    }

    public long getSDUsedLong() {
        return sdBlockSize * (sdBlockCount - sdAvailableBlocks);
    }

    public String getInternalTotal(Context context) {
        return formatSize(context, sdBlockSize * sdBlockCount);
    }

    public long getInternalTotalLong() {
        return sdBlockSize * sdBlockCount;
    }

    public String getExternalAvailable(Context context) {
        return formatSize(context, exBlockSize * exAvailableBlocks);
    }

    public String getExternalUsed(Context context) {
        return formatSize(context, exBlockSize * (exBlockCount - exAvailableBlocks));
    }

    public long getExternalUsedSize() {
        return exBlockSize * (exBlockCount - exAvailableBlocks);
    }

    public String getExternalTotal(Context context) {
        return formatSize(context, exBlockSize * exBlockCount);
    }

    public long getExternalTotalSize() {
        return exBlockSize * exBlockCount;
    }

    private String formatSize(Context context, long l) {
        return Formatter.formatFileSize(context, l);
    }

    public long getSDAvailableLong() {
        return sdBlockSize * sdAvailableBlocks;
    }
}
